/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ejb;

import entities.Category;
import entities.User;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;



/**
 *
 * @author Alejandro
 */
@Stateless
public class UserBean implements UserBeanLocal {

    @PersistenceContext(unitName="Retailshop-ejbPU")
   // @PersistenceUnit
    EntityManager manager;
   

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    @Override
    public void addUser(User user) {
//        manager.getTransaction().begin();
        manager.merge(user);
//        manager.getTransaction().commit();
    }

    @Override
    public User findByEmail(User user) {
        User res = null;
        Query query = manager.createNamedQuery("User.findByEmail");
        query.setParameter("email", user.getEmail());
        try {
            res = (User) query.getSingleResult();
        } catch(NoResultException e) {
            return null;
        }
        return res;
    }

    @Override
    public List<User> findAll(){
        List<User> res = null;
        Query query = manager.createNamedQuery("User.findAll");
      
        try {
            res = (List<User>) query.getResultList();
        } catch (NoResultException e) {
            return null;
        }
        return res;
    }
  

}
